<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mrestaurantcategories extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    /**
     * 
     * @void Country
     * 
     */
    
    public function getRetaurantCategoriesDB($baseurl,$number,$offset)
    {
        $html = $del = $check = '';
        $limit = $offset ? intval($offset) : '0';
        $sql = "SELECT * FROM restaurant_categories WHERE 1=1 AND parent_id = 0 ";
        $key = $this->getParamString('keyword');
        if(!in_array($key, array(null, '', '0')))
        {
            $sql .= " AND (title like '%%".$key."%%' OR ";
            $sql .= " title_ascii like '%%".$key."%%') ";
        }
        $sql .= "ORDER BY id DESC";
        $xdata = " LIMIT ".$limit.",".$number." ";
        $data = $this->db->query($sql.$xdata)->result_array();
        
        if($data)
        {
            $i = 1;
            foreach($data AS $le=>$el)
            {
                $child = '|--- ';

                $html .= '<tr>
                                <td class="centeralign"><input type="checkbox" id="'.$el['id'].'" class="nhatnv" name="checkbox[]" value="'.$el['id'].'" /></td>
                                <td>'.$el['title'].'</td>
                                <td class="center">
                                <a href="'.$baseurl.'manage/restaurant/edit-categories/'.$el['id'].'"><span class="icon-edit"></span></a>
                                <a href="'.$baseurl.'manage/restaurant/delete-categories/'.$el['id'].'" class="delete-a"><span class="icon-trash"></span></a>
                                </td>
                            </tr>';
                $html .= $this->subgetdata($el['id'],$baseurl,$child);
                
            }
        }
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $html, 'count' => $count); 
    }
    
    private function subgetdata($id,$baseurl,$child)
    {
        $html = ''; @$child = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$child;
        $data = $this->db->query('SELECT * FROM restaurant_categories WHERE parent_id = "'.$id.'" ORDER BY id ASC')->result_array();

        if($data)
        {
            foreach($data AS $le=>$el)
            {
                $html .= '<tr class="" style="background: #efefef;">
                            	<td align="center" style="padding-left: 12px;"><input type="checkbox" id="'.$el['id'].'" class="nhatnv" name="checkboxs[]" value="'.$el['id'].'" /></td>
                                <td>'.$child.$el['title'].'</td>
                                <td class="center">
                                <a href="'.$baseurl.'manage/restaurant/edit-categories/'.$el['id'].'"><span class="icon-edit"></span></a>
                                <a href="'.$baseurl.'manage/restaurant/delete-categories/'.$el['id'].'" class="delete-a"><span class="icon-trash"></span></a>
                                </td>
                            </tr>';
                
                $html .= $this->subgetdata($el['id'],$baseurl,$child);
                
            }
        }
        return $html;
    }
    
    public function getRetaurantCategoriesDB_($number,$offset)
    {
        
        $limit = $offset ? intval($offset) : '0';
        
        $sql = "SELECT * FROM restaurant_categories WHERE 1=1 ";
        $key = $this->getParamString('keyword');
        if(!in_array($key, array(null, '', '0')))
        {
            $sql .= " AND (title like '%%".$key."%%' OR ";
            $sql .= " title_ascii like '%%".$key."%%') ";
        }
        $sql .= "ORDER BY id DESC";
        
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);   
    }
    
    public function addRetaurantCategoriesDb($admin_id)
    {
        $title = $this->getParamString('title');
        $title_ascii = $this->get_ascii($title);
        
        $arr = array(
                'title'         => $title,
                'title_ascii'   => $title_ascii,
                'admin_id'      => $admin_id,
                'parent_id'     => $this->getParamInt('parent'),
                'created'       => date('Y-m-d H:i:s')
            );
        $this->db->insert('restaurant_categories', $arr);
        return 200;
    }
    public function editRetaurantCategoriesDb($admin_id, $id)
    {
        $title = $this->getParamString('title');
        $title_ascii = $this->get_ascii($title);
        
        $arr = array(
                'title'         => $title,
                'title_ascii'   => $title_ascii,
                'admin_id'      => $admin_id,
                'parent_id'     => $this->getParamInt('parent'),
                'created'       => date('Y-m-d H:i:s')
            );
        $this->db->update('restaurant_categories', $arr, "id = ".$id);
        return 200;
    }
    
    public function deleteRetaurantCategoriesDB($id)
    {
        $check_child = $this->db->query("SELECT id FROM restaurant_categories WHERE parent_id IN ('" . (is_array($id) ? implode("','", $id) : $id) . "')")->num_rows();
        {
            if($check_child > 0)
            {
                $check_content = $this->db->query("SELECT id FROM restaurant_categories_key WHERE id_categories IN ('" . (is_array($id) ? implode("','", $id) : $id) . "')")->num_rows();
                if($check_content > 0)
                {
                    return 202;
                }
                return 201;
            }
        }
        $this->db->query("DELETE FROM restaurant_categories WHERE id IN ('" . (is_array($id) ? implode("','", $id) : $id) . "')");
        return 200;
        
    }
    
    public function getRetaurantCategoriesDetailDB($id)
    {
        return $this->db->query('SELECT * FROM restaurant_categories WHERE id = '.$id)->row_object();
    }
    
    public function optionParent($id)
    {
        $data = $this->db->query('SELECT id, title FROM restaurant_categories WHERE parent_id = 0 ORDER BY title ASC')->result_object();
        $html = '<option value = "">Chọn danh mục cha</option>';
        $html .= '<option value = "0">Đây là danh mục cấp cha</option>';
        foreach($data AS $el=>$le)
        {
            if($le->id == $id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$le->id.'" '.$selected.'>'.$le->title.'</option>';
        }
        return $html;
    }
    
}
